package book.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import book.dao.BaseDao;
import book.dao.BookDao;
import book.util.Book;



public class BookDaoImpl implements BookDao{
	private Connection connection;
	private PreparedStatement pstmt;
	private ResultSet rs;
	public int insertinto(Book book) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="insert into book(Book_id,Type,Title,Publisher,Year,Author,Price,number,Stocks)values(?,?,?,?,?,?,?,?,?)";
		pstmt=connection.prepareStatement(sql);
		pstmt.setInt(1, book.getBook_id());
		pstmt.setString(2, book.getType());
		pstmt.setString(3, book.getTitle());
		pstmt.setString(4, book.getPublisher());
		pstmt.setInt(5, book.getYear());
		pstmt.setString(6, book.getAuthor());
		pstmt.setInt(7, book.getPrice());
		pstmt.setInt(8, book.getNumber());
		pstmt.setInt(9, book.getStocks());
		
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}
	public int deletefrom(int Book_id) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="delete from book where Book_id=?"; 
		pstmt=connection.prepareStatement(sql);
		pstmt.setInt(1, Book_id);
	
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}
	
	public int updateset(Book book) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="update book set Type=?,Title=?,Publisher=?,Year=?,Author=?,Price=?,number=?,Stocks=? where Book_id=?";
		pstmt=connection.prepareStatement(sql);
		pstmt.setInt(9, book.getBook_id());
		pstmt.setString(1, book.getType());
		pstmt.setString(2, book.getTitle());
		pstmt.setString(3, book.getPublisher());
		pstmt.setInt(4, book.getYear());
		pstmt.setString(5, book.getAuthor());
		pstmt.setInt(6, book.getPrice());
		pstmt.setInt(7, book.getNumber());
		pstmt.setInt(8, book.getStocks());
		
		num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}
	
	public List<Book> findBookAll() {
	List<Book> bklist=new ArrayList<Book>();
	try {
	connection=BaseDao.getConnection();
	String sql="select Book_id,Type,Title,Publisher,Year,Author,Price,number,Stocks from book";
	
		pstmt=connection.prepareStatement(sql);
		rs=pstmt.executeQuery();
		while(rs.next()){
			Book book=new Book(rs.getInt("book_id"),
					rs.getString("type"),rs.getString("title"),rs.getString("publisher"),
					rs.getInt("year"),rs.getString("author"),rs.getInt("price"),
					rs.getInt("number"),rs.getInt("stocks"));
			bklist.add(book);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		BaseDao.closeAll(rs, pstmt, connection);
	}
		return bklist;
	}
}
